﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;

namespace ENG.EngStartQueueLib
{
  public static class RegistryAPI
  {
    public struct RegistryPath
    {
      public readonly eRoot Root;
      public readonly string SubKey;
      public RegistryPath(string path)
      {
        eRoot r;
        string s;
        AnalysePath(path, out r, out s);
        this.Root = r;
        this.SubKey = s;
      }
      public RegistryPath(eRoot root, string subKey)
      {
        this.Root = root;
        this.SubKey = subKey;
      }
    }

    public static void AnalysePath(string key, out eRoot root, out string subKey)
    {
      if (key.StartsWith(eRoot.HKCR.ToString()))
      {
        root = eRoot.HKCR;
        subKey = key.Substring(5);
      }

      else if (key.StartsWith(eRoot.HKCU.ToString()))
      {
        root = eRoot.HKCU;
        subKey = key.Substring(5);
      }

      else if (key.StartsWith(eRoot.HKLM.ToString()))
      {
        root = eRoot.HKLM;
        subKey = key.Substring(5);
      }

      else if (key.StartsWith(eRoot.HKU.ToString()))
      {
        root = eRoot.HKU;
        subKey = key.Substring(4);
      }

      else if (key.StartsWith(eRoot.HKCC.ToString()))
      {
        root = eRoot.HKCC;
        subKey = key.Substring(5);
      }

      else
        throw new Exception("Key is not valid registry key. No prefix recognized.");
    }

    public enum eRoot
    {
      HKCR,
      HKCU,
      HKLM,
      HKU,
      HKCC
    }

    public static RegistryKey GetRootKey(eRoot root)
    {
      RegistryKey ret = null;
      switch (root)
      {
        case eRoot.HKCC:
          ret = Registry.CurrentConfig;
          break;
        case eRoot.HKCR:
          ret = Registry.ClassesRoot;
          break;
        case eRoot.HKCU:
          ret = Registry.CurrentUser;
          break;
        case eRoot.HKLM:
          ret = Registry.LocalMachine;
          break;
        case eRoot.HKU:
          ret = Registry.Users;
          break;
      }

      return ret;
    }

    public static RegistryKey GetKey(string fullKey)
    {
      RegistryPath path = new RegistryPath(fullKey);
      RegistryKey ret = GetRootKey(path.Root).OpenSubKey(path.SubKey);

      return ret;
    }
    public static RegistryKey GetKey(eRoot root, string subKey)
    {
      RegistryPath path = new RegistryPath(root, subKey);
      RegistryKey ret = GetKey(path);

      return ret;
    }
    public static RegistryKey GetKey(RegistryPath path)
    {
      RegistryKey ret = GetRootKey(path.Root).OpenSubKey(path.SubKey);

      return ret;
    }

    public static string[] GetValueNames(string key)
    {
      RegistryKey regkey = GetKey(key);
      string[] ret = regkey.GetValueNames();
      return ret;
    }
    public static string[] GetKeyItems(eRoot root, string subKey)
    {
      RegistryKey regkey = GetKey(root, subKey);
      string[] ret = regkey.GetValueNames();
      return ret;
    }

    public static void Insert(string key, string value)
    {
      string vn = System.IO.Path.GetFileName(key);
      key = System.IO.Path.GetDirectoryName(key);
      RegistryPath rp = new RegistryPath(key);
      Insert(rp.Root, rp.SubKey, vn, value);
    }
    public static void Insert(eRoot root, string subKey, string valueName, string value)
    {
      RegistryKey rk = GetRootKey(root);
      rk = rk.CreateSubKey(subKey);
      rk.SetValue(valueName, value);
    }
  }
}
